package org.game.module.scene
{
	import com.greensock.TweenNano;
	import com.greensock.easing.Circ;
	import com.greensock.easing.Linear;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import org.game.common.Common;
	import org.game.module.scene.Avatar.CellAvatar;
	import org.game.module.scene.elements.IMicroElement;
	import org.game.module.scene.elements.Mineral;
	import org.game.module.scene.layer.BackgroundLayer;
	import org.game.utils.Initialization;
	
	
	/**
	 * @author 缺硒
	 */
	public class MicroScene extends Sprite
	{
		private var _backgoundLayer:BackgroundLayer;
		private var _playerCell:CellAvatar;
		private var _viewRect:Rectangle;
		private var _elementVector:Vector.<IMicroElement>;
		
		// 移动目标点
		private var _targetPoint:Point = new Point(0,0);
		
		public function MicroScene()
		{
			_backgoundLayer = Initialization.init(new BackgroundLayer(), this);
			_elementVector = new Vector.<IMicroElement>();
			
			for(var i:int = 0; i < 10; i ++)
			{
				var mineral:Mineral = new Mineral();
				mineral.x = Math.random() * 500 + 100;
				mineral.y = Math.random() * 400 + 100;
				addChild(mineral);
				_elementVector.push(mineral);
			}
		}
		
		public function addPlaerCell():void
		{
			_playerCell = Initialization.init(new CellAvatar(), this);
			_viewRect = new Rectangle(0,0,Common.viewWidth, Common.viewHeight);
			scrollRect = _viewRect;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			_backgoundLayer.addEventListener(MouseEvent.CLICK, onClickBg);
		}
		
		private function onEnterFrame(evt:Event):void
		{
			
			_viewRect.x+=(_playerCell.x-Common.viewWidth / 2-_viewRect.x)*0.1;
			_viewRect.y+=(_playerCell.y-Common.viewHeight / 2-_viewRect.y)*0.1;
			
			scrollRect = _viewRect;
			
			for each(var mineral:* in _elementVector)
			{
				if(_playerCell.hitTestObject(mineral))
				{
					_playerCell.size += 3;
					_elementVector.splice(_elementVector.indexOf(mineral), 1);
					this.removeChild(mineral);
				}
			}
		}
		 
		private function onClickBg(evt:MouseEvent):void
		{
			_targetPoint.x = mouseX;
			_targetPoint.y = mouseY;
			TweenNano.to(_playerCell, 1, {x:mouseX, y:mouseY});
		}
		//
	}
}